import store from '../store.js'
import {
    getSign
} from '@/utils/sign.js'
import {
    getStorage,
    getTime
} from '@/utils/utils.js'
module.exports = (params) => {
    let url = `${store.getters.BASEURL}${params.url}`;
    let method = params.method;
    let header = params.header || {};
    let data = params.data || {};
    let sign = "";

    if (!method) {
        method = "POST"
    } else {
        method = method.toLocaleUpperCase()
    }

    // 签名
    sign = getSign(data);
    if (sign != '') {
        data['_sign'] = sign;
    }

    // header['x-token'] = getStorage('x-token') || null
    header['Content-Type'] = 'application/x-www-form-urlencoded' || null

    //	发起网络请求
    if (!params.url) {
        return
    }

    return new Promise((resolve, reject) => {

        if (params.hideLoad) {
            uni.showLoading({
                mask: true,
            })
        }
        uni.request({
            url: url,
            method: method || "GET",
            header: header,
            data: data,
            dataType: "json",
            sslVerify: false, //	是否验证ssl证书
            success: res => {

                console.log(res, '==>> res 接口res');
                console.log(data, '==>> res 接口data');

                uni.hideLoading()
                if (res.data.code == -100) {
                    // 未登录重定向
                    uni.redirectTo({
                        url: `/pages/login/login`
                    })
                    return
                }

                if (res.statusCode && res.statusCode != 200) {
                    uni.showToast({
                        title: '服务器出了一些问题~',
                        icon: 'none',
                        duration: 3000
                    })
                    return;
                }

                if (res.data.code == -1) {
                    uni.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                    return
                }
                resolve(res.data)

            },
            fail: err => {
                console.log(err, '==>> err 接口err');
                setTimeout(() => {
                    uni.hideLoading()
                }, 300)
                reject(err)
            }
        })

    })

}
